package com.isoft.dao.user;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isoft.entity.user.User;
import com.isoft.entity.vo.LoginParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:用户数据层
 * @date 2024/6/3 11:47
 */
@Mapper
public interface UserDao extends BaseMapper<User>{
//    查询所有用户
    @Select("select * from TEAM04.T_CUSTOMER")
    List<User> selectAll();
//  根据用户名密码查询用户
    @Select("select * from TEAM04.T_CUSTOMER where username=#{username} and userpass=#{userpass}")
    User selectOne(String username, String userpass);

//    会员查询
    @Select("SELECT C.* FROM TEAM04.T_CUSTOMER AS C JOIN TEAM04.T_CUSTOMER_AUTHORITY AS CA ON C.id = CA.customer_id JOIN TEAM04.T_AUTHORITY AS A ON A.id = CA.authority_id WHERE C.username=#{username} and C.userpass=#{userpass} and A.authority = 'ROLE_vip' ")
    User selectVip(String username, String userpass);
//    会员查询:根据用户名查询用户selectVipByUsername
    @Select("SELECT C.*,A.authority FROM TEAM04.T_CUSTOMER AS C JOIN TEAM04.T_CUSTOMER_AUTHORITY AS CA ON C.id = CA.customer_id JOIN TEAM04.T_AUTHORITY AS A ON A.id = CA.authority_id WHERE C.username=#{username} and A.authority = 'ROLE_vip' ")
    User selectVipByUsername(String username);
//    普通用户查询
    @Select("SELECT C.* FROM TEAM04.T_CUSTOMER AS C JOIN TEAM04.T_CUSTOMER_AUTHORITY AS CA ON C.id = CA.customer_id JOIN TEAM04.T_AUTHORITY AS A ON A.id = CA.authority_id WHERE C.username=#{username} and C.userpass=#{userpass} and A.authority ='ROLE_common'")
    User selectCommonUser(String username, String userpass);
//    普通用户查询:根据用户名查询用户
    @Select("SELECT C.*,A.authority FROM TEAM04.T_CUSTOMER AS C JOIN TEAM04.T_CUSTOMER_AUTHORITY AS CA ON C.id = CA.customer_id JOIN TEAM04.T_AUTHORITY AS A ON A.id = CA.authority_id WHERE C.username=#{username} and A.authority ='ROLE_common'")
    User selectCommonUserByUsername(String username);
//  添加用户
    @Insert("insert into TEAM04.T_CUSTOMER(username,userpass,email) values(#{username},#{userpass},#{email})")
    int insert(User user);
//    根据添加的用户添加默认的权限（ROLE_common）
    @Insert("insert into TEAM04.T_CUSTOMER_AUTHORITY(customer_id) values((select id from TEAM04.T_CUSTOMER where username=#{username} ))")
    int insertAuthority(String username);
//    修改用户名、密码
    @Update("update TEAM04.T_CUSTOMER set userpass=#{userpass} where email=#{email}")
    int update(LoginParam loginParam);

    //    修改用户名、邮箱、valid
    @Update("update TEAM04.T_CUSTOMER set username=#{username},email=#{email},valid=#{valid} where id=#{id}")
    int updateUserInfo(User user);

    //修改权限
    @Update("update TEAM04.T_CUSTOMER_AUTHORITY set authority_id=#{authority_id} where customer_id=#{userid}")
    int updateAuthority(Integer userid,Integer authority_id);

    //查询权限
    @Select("select authority_id from TEAM04.T_CUSTOMER_AUTHORITY where customer_id=#{userid}")
    int selectAuthority(Integer userid);

    //用户名模糊查询用户
    @Select("SELECT C.ID, C.USERNAME, C.EMAIL, A.AUTHORITY, C.VALID " +
                    "FROM TEAM04.T_CUSTOMER C " +
                    "INNER JOIN TEAM04.T_CUSTOMER_AUTHORITY CA ON C.ID = CA.CUSTOMER_ID " +
                    "INNER JOIN TEAM04.T_AUTHORITY A ON CA.AUTHORITY_ID = A.ID " +
                    "WHERE C.USERNAME LIKE CONCAT('%', #{USERNAME}, '%')")
    List<User> selectUserByUsername(@Param("USERNAME") String username);

    //删除用户
    @Delete("delete from TEAM04.T_CUSTOMER where id=#{id}")
    int delete(Integer id);

    /**
     * 分页查询
     */
 @Select("SELECT C.ID, C.USERNAME, C.EMAIL, A.AUTHORITY, C.VALID " +
            "FROM TEAM04.T_CUSTOMER C " +
            "INNER JOIN TEAM04.T_CUSTOMER_AUTHORITY CA ON C.ID = CA.CUSTOMER_ID " +
            "INNER JOIN TEAM04.T_AUTHORITY A ON CA.AUTHORITY_ID = A.ID " +
            " order by id desc limit #{currentPage},#{pageSize}")
    List<User> selectUserInfoByPage(@Param("currentPage") int currentPage, @Param("pageSize") int pageSize);
 @Select("select count(*) from TEAM04.T_CUSTOMER")
    Long selectUserInfoCount();


}
